package cn.ycms.web.action.admin.Permi;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.ycms.bean.Manager;
import cn.ycms.jdbc.JdbcUtil;
import cn.ycms.service.ManagerService;
import cn.ycms.utils.CommonMethod;
import cn.ycms.utils.MapUtils;
import cn.ycms.utils.QueryModel;
import cn.ycms.utils.StringUtils;
import cn.ycms.utils.dateUtils;
import cn.ycms.utils.datebean;
import cn.ycms.web.action.Action;

@Controller
public class ManagerAction extends Action {
	@Resource
	private ManagerService managerService;

	@RequestMapping("/permi/manageradd")
	public void ManagerAdd(HttpServletRequest request) {

	}

	@RequestMapping("/permi/managersaveorupdate")
	public String ManagerAddsave(HttpServletRequest request, Manager manager) {
		managerService.save(manager);
		return "redirect:/permi/managerlist.do";
	}

	@RequestMapping("/permi/managerlist")
	public void Managerlist(HttpServletRequest request,
			Map<String, Object> parms)

	{

		int start = 0;
		int limit = 10;
		datebean datebean = new datebean();
		parms = MapUtils.NonullMapKey(parms, request.getParameter("select"),
				"%" + request.getParameter("name") + "%");
		request.setAttribute("name", request.getParameter("name"));

		if (request.getParameter("Start") != null) {
			start = StringUtils.getPositiveInt(new Integer(request
					.getParameter("Start")));
		
		}
		if (request.getParameter("Limit") != null) {
			limit = StringUtils.getPositiveInt(new Integer(request
					.getParameter("Limit")), 10);
		}
		

		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MM/dd/yyyy");
		Date Startdate = null;
		Date endDate = null;

		try {
			if (!StringUtils.isNull(request.getParameter("Startcreatedates"))) {
				Startdate = simpleDateFormat.parse(request.getParameter(
						"Startcreatedates").toString());
				request.setAttribute("Startdate", request.getParameter(
				"Startcreatedates").toString());

			}
			if (!StringUtils.isNull(request.getParameter("endcreatedates"))) {
				endDate = simpleDateFormat.parse(request.getParameter(
						"endcreatedates").toString());
				request.setAttribute("endcreatedates", request.getParameter(
				"endcreatedates").toString());

			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		datebean.setPropertyName("createdates");
		datebean.setStartdate(Startdate);
		datebean.setEndDate(endDate);
		
		int count = managerService.getRowCount(parms, datebean);
		
		if(start==count/limit)
		{
			limit=count%limit;
		}
		request.setAttribute("list", managerService.GetEntitybyPage(parms,
				datebean, start, limit));
	    limit=10;
		request.setAttribute("count", count);
        request.setAttribute("start", start);
		request.setAttribute("page", count / limit + 1);
		request.setAttribute("limit", limit);

	}

}
